<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="btnCancle">
    <!-- 表单 -->
    <el-form ref="addForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width: 88%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width: 88%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 88%"
          placeholder="请选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 88%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in EnmuData.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 88%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          style="width: 88%"
          placeholder="请选择部门"
          @focus="showDepartments"
        />
        <el-tree
          v-if="showTree"
          class="elTree"
          :data="departmentList"
          :props="{label: 'name' }"
          :default-expand-all="true"
          @node-click="selectLabel"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 50%"
          placeholder="请选择日期"
        />
      </el-form-item>
    </el-form>
    <!-- 底部按钮 -->
    <el-row type="flex" justify="center" align="middle">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancle">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { dataTransform } from '@/utils'
import EnmuData from '@/api/constant/employees'
import { addEmployee } from '@/api/employee'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 表单数据
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      // 表单校验规则
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      },
      // 部门列表
      departmentList: [],
      // 是否显示树状菜单
      showTree: false,
      // 枚举数据
      EnmuData
    }
  },
  methods: {
    // 显示树形
    async showDepartments() {
      const { depts } = await getDepartments()
      this.departmentList = dataTransform(depts, '')
      this.showTree = true
    },
    // 树形点击事件
    selectLabel(val) {
      this.formData.departmentName = val.name
      this.showTree = false
    },
    // 确定按钮
    async btnOK() {
      // 二次校验
      await this.$refs.addForm.validate()
      // 发请求
      await addEmployee(this.formData)
      // 提示
      this.$message.success('新增成功')
      // 刷新页面
      this.$parent.getList()
      // 关闭弹窗
      this.$parent.showDialog = false
    },
    // 取消按钮
    btnCancle() {
      // 重置数据
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 清除校验结果
      this.$refs.addForm.resetFields()
      // 关闭弹窗
      this.$parent.showDialog = false
    }
  }
}
</script>

<style scoped>
.elTree {
  height: 150px;
  overflow: auto;
}
</style>
